package com.q.tqb.activity.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MineUpdateNameActivity extends BaseActivity {


    @BindView(R.id.et_nickname)
    EditText mEtNickname;
    @BindView(R.id.tv_update)
    TextView mTvUpdate;
    private String nickname;
    private int uid;
    private int isShow;
    private int type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_update_name;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        uid = (int) SPUtil.get(this, "uid", 0);
        isShow = (int) SPUtil.get(this, "isShow", 0);
        type = getIntent().getIntExtra("type", 1);

        if (type == 1) {
            setTitle("修改昵称");
        } else {
            setTitle("修改签名");
        }
        nickname = getIntent().getStringExtra("content");
        mEtNickname.setText(nickname);

    }


    @OnClick({R.id.iv_back, R.id.tv_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_update:
                nickname = mEtNickname.getText().toString();
                if (TextUtils.isEmpty(nickname)) {
                    showToast("内容不能为空");
                } else {
                    update();
                }
                break;
        }
    }

    private void update() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        map.put("is_show_privacy", isShow+"");
        if (type == 1) {
            map.put("nickname", nickname);
        } else {
            map.put("signature", nickname);
        }
        OkHttpUtil.getInstance().post(MineUpdateNameActivity.this, AppConst.URL_UPDATE_INFO, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                showToast("修改成功");
                if (type == 1) {
                    SPUtil.put(MineUpdateNameActivity.this, "nickname", nickname);
                } else {
                    SPUtil.put(MineUpdateNameActivity.this, "signature", nickname);
                }
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                showToast(errorMsg);
            }
        });
    }

}
